﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SysApp
    {
        public int Add(KeRui.Model.SysApp model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@AppName", SqlDbType.NVarChar, 50), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@IsSystem", SqlDbType.Int, 4), new SqlParameter("@AppUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@AppKey", SqlDbType.VarChar, 50), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@CreateTime", SqlDbType.DateTime) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.AppName;
            parameters[2].Value = model.ModelID;
            parameters[3].Value = model.IsSystem;
            parameters[4].Value = model.AppUrl;
            parameters[5].Value = model.AppKey;
            parameters[6].Value = model.IsUsing;
            parameters[7].Value = model.CreateTime;
            DbHelperSQL.RunProcedure("SysApp_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int Delete(int AppID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4) };
            parameters[0].Value = AppID;
            return DbHelperSQL.RunProcedure("SysApp_Delete", parameters, out num);
        }

        public KeRui.Model.SysApp GetModel(int AppID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4) };
            parameters[0].Value = AppID;
            KeRui.Model.SysApp app = new KeRui.Model.SysApp();
            DataSet set = DbHelperSQL.RunProcedure("SysApp_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["AppID"].ToString() != "")
                {
                    app.AppID = int.Parse(set.Tables[0].Rows[0]["AppID"].ToString());
                }
                app.AppName = set.Tables[0].Rows[0]["AppName"].ToString();
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    app.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsSystem"].ToString() != "")
                {
                    app.IsSystem = int.Parse(set.Tables[0].Rows[0]["IsSystem"].ToString());
                }
                app.AppUrl = set.Tables[0].Rows[0]["AppUrl"].ToString();
                app.AppKey = set.Tables[0].Rows[0]["AppKey"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsUsing"].ToString() == "1") || (set.Tables[0].Rows[0]["IsUsing"].ToString().ToLower() == "true"))
                    {
                        app.IsUsing = true;
                    }
                    else
                    {
                        app.IsUsing = false;
                    }
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    app.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return app;
            }
            return null;
        }

        public int Update(KeRui.Model.SysApp model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@AppName", SqlDbType.NVarChar, 50), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@IsSystem", SqlDbType.Int, 4), new SqlParameter("@AppUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@AppKey", SqlDbType.VarChar, 50), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@CreateTime", SqlDbType.DateTime) };
            parameters[0].Value = model.AppID;
            parameters[1].Value = model.AppName;
            parameters[2].Value = model.ModelID;
            parameters[3].Value = model.IsSystem;
            parameters[4].Value = model.AppUrl;
            parameters[5].Value = model.AppKey;
            parameters[6].Value = model.IsUsing;
            parameters[7].Value = model.CreateTime;
            return DbHelperSQL.RunProcedure("SysApp_Update", parameters, out num);
        }
    }
}

